<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs
          v-model="activeName"
          @tab-click="handleClick"
        >
          <el-tab-pane
            label="角色管理"
            name="first"
          >
            <el-button icon="plus" type="primary" size="small" @click="showDialog=true">新增角色</el-button>
            <!-- 表格 -->
            <el-table :data="list" border="">
              <el-table-column label="序号" width="120px" type="index" />
              <el-table-column label="角色名称" width="240px" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="{ row }">
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="success" @click="deleteRole(row.id)">删除</el-button>
                  <el-button size="small" type="warning" @click="assignPermission(row.id)">权限</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页 -->
            <div class="block" type="flex" justify="center" align="center">
              <el-pagination
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                layout="prev, pager, next"
                @current-change="changePage"
              />
            </div>
          </el-tab-pane>
          <el-tab-pane
            label="公司信息"
            name="second"
          >
            <template>
              <el-alert
                type="info"
              >
                <i class="el-icon-info" />
                对公司名称、公司地址、公司营业执照、公司地区的更新
              </el-alert>

            </template>
            <el-form ref="form" :model="formData" label-width="120px" style="margin-top:20px" disabled>
              <el-form-item label="公司名称" style="width:400px">
                <el-input v-model="formData.name" />
              </el-form-item>
              <el-form-item label="公司地址" style="width:400px">
                <el-input v-model="formData.companyAddress" />
              </el-form-item>
              <el-form-item label="邮箱" style="width:400px">
                <el-input v-model="formData.mailbox" />
              </el-form-item>
              <el-form-item label="备注" style="width:400px">
                <el-input v-model="formData.remarks" type="textarea" :rows="3" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 弹层 -->
    <el-dialog :title="titleShow" :visible="showDialog" @close="btnCancel">
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 权限弹层 -->
    <el-dialog
      title="分配权限"
      :visible.sync="showDialogPremission"
    >
      <el-tree
        ref="permissionRefs"
        :data="permissionList"
        :show-checkbox="true"
        :check-strictly="true"
        node-key="id"
        :default-expand-all="true"
        :default-checked-keys="selectCheck"
        :props="defaultProps"
      />
      <span slot="footer" class="dialog-footer">
        <el-row type="flex" justify="center">
          <el-col :span="6">
            <el-button @click="premissionCancel">取 消</el-button>
            <el-button type="primary" @click="submitPermission">确 定</el-button>
          </el-col>
        </el-row>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole, assignPerm } from '@/api/settings'
import { mapGetters } from 'vuex'
import { getPermissionList } from '@/api/premission'
import { tranListToTreeData } from '@/utils'
export default {
  data() {
    return {
      roleForm: {
        name: '',
        description: ''
      },
      rules: { name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }] },
      showDialog: false,
      activeName: 'first',
      formData: {},
      page: {
        total: 0,
        page: 1,
        pagesize: 10
      },
      list: [],
      showDialogPremission: false,
      permissionList: [],
      defaultProps: {
        label: 'name'
      },
      selectCheck: [],
      roleId: null
    }
  },
  computed: {
    ...mapGetters(['companyId']),
    // 修改弹层提示
    titleShow() {
      return this.roleForm.id ? '编辑角色' : '新增角色'
    }
  },
  created() {
    // 先初始化 获取数据
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    // 编辑角色
    async editRole(id) {
      this.roleForm = await getRoleDetail(id) // 数据的回显
      this.showDialog = true // 为了不出现闪烁的问题 先获取数据 再弹出层
    },
    async btnOK() {
      try {
        this.$refs.roleForm.validate()
        // 新增 还是 编辑
        if (this.roleForm.id) {
        // 有id去编辑 提交数据
          await updateRole(this.roleForm)
          // 重新拉取数据
          this.getRoleList()
        } else {
        // 去新增
          await addRole(this.roleForm)
          this.getRoleList()
        }
        this.showDialog = false
        this.$message.success('修改成功')
      } catch (err) {
        console.log(err)
      }
    },
    btnCancel() {
      // 清空表单 跟校验规则
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    handleClick(tab, event) {
      console.log(tab, event)
    },
    // 获取角色列表
    async getRoleList() {
      const { rows, total } = await getRoleList(this.page)
      this.list = rows
      this.page.total = total
    },
    // 触发改变页数
    changePage(newPage) {
      this.page.page = newPage
      // 重新拉取数据
      this.getRoleList()
    },
    // 获取公司信息
    async getCompanyInfo() {
      // 请求中的companyId来自哪里？它来自我们登录成功之后的用户资料，所以我们需要在该组件中使用vuex数据
      this.formData = await getCompanyInfo(this.companyId)
    },
    // 删除数据
    async deleteRole(id) {
      try {
        await this.$confirm('确认删除角色吗？')
        await deleteRole(id)
        this.getRoleList()
        this.$message.success('删除成功')
      } catch (err) {
        console.log(err)
      }
    },
    // 点击添加权限管理的弹层
    async assignPermission(id) {
      // 获取员工数据列表
      const res = await getPermissionList()
      // 转树结构
      this.permissionList = tranListToTreeData(res, '0')
      // 获取默认数据
      const { permIds } = await getRoleDetail(id)
      // console.log(data)
      this.selectCheck = permIds
      // 打开弹层
      this.showDialogPremission = true
      // 获取到当前点击的id
      this.roleId = id
    },
    // 点击确定提交数据
    async submitPermission() {
      // 提交数据
      // 获取到数据
      // 树节点选中的id列表
      // 修改的是哪个角色
      const permIds = this.$refs.permissionRefs.getCheckedKeys()
      await assignPerm({
        permIds,
        id: this.roleId
      })
      this.$message.success('分配成功')
      this.showDialogPremission = false
    },
    // 点击取消关闭权限的弹层
    premissionCancel() {
      this.showDialogPremission = false
    }
  }
}
</script>

<style>
</style>
